package sisi.invoices;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.zkoss.zk.ui.Executions;
import org.zkoss.zk.ui.Sessions;
import org.zkoss.zk.ui.SuspendNotAllowedException;
import org.zkoss.zk.ui.event.CreateEvent;
import org.zkoss.zk.ui.event.Event;
import org.zkoss.zk.ui.event.ForwardEvent;
import org.zkoss.zk.ui.sys.ComponentsCtrl;
import org.zkoss.zk.ui.util.ConventionWires;
import org.zkoss.zul.Checkbox;
import org.zkoss.zul.Datebox;
import org.zkoss.zul.Doublebox;
import org.zkoss.zul.Intbox;
import org.zkoss.zul.ListModelList;
import org.zkoss.zul.Listbox;
import org.zkoss.zul.Listcell;
import org.zkoss.zul.Listitem;
import org.zkoss.zul.ListitemRenderer;
import org.zkoss.zul.Messagebox;
import org.zkoss.zul.Textbox;
import org.zkoss.zul.Toolbarbutton;
import org.zkoss.zul.Window;

import sisi.General;

@SuppressWarnings({ "rawtypes", "unchecked", "serial" })
public class SuppinvoicedetailWindow extends Window implements 
				org.zkoss.zk.ui.ext.AfterCompose {
//window
private Window windowSuppinvoicedetail, windowEditSuppinvoicedetail;
private Textbox tbCerca;
private Listbox boxSuppinvoicedetail;
private List lista_Suppinvoicedetail;
private boolean lNuovo;
private Toolbarbutton buttonSeleziona;
private ListModelList listModelList;
private Integer idSelezionato = 0;

protected Map<String, Object> args;
private Object SuppinvoicedetailItem;
private Suppinvoicedetail modSuppinvoicedetail;
// edit
private Textbox type;
private Textbox note;
private Intbox suppid;
private Doublebox suppvat;
private Textbox curr;
private Doublebox unitprice;
private Textbox stat;
private Textbox partno;
private Textbox productname;
private Textbox um;
private Intbox seq;
private Intbox productid;
private Doublebox quantity;
private Doublebox suppvatperc;
private Intbox recnum;
private Intbox custid;
private Textbox custname;
private Datebox dtup;
private Doublebox exchangerate;
private Textbox nfysnote;
private Intbox order;
private Checkbox paid;
private Doublebox paidamount;
private Textbox suppname;
private Textbox suppnuminvoice;
private Textbox tdoc;
private Checkbox billitem;
private Datebox paiddt;
private Doublebox supptaxed;
private Doublebox totalamount;
private Textbox vatarticle;
private Textbox vatcode;

		public void onCreate$windowSuppinvoicedetail(Event event) {

		CreateEvent ce = (CreateEvent) ((ForwardEvent) event).getOrigin();
		args = (Map<String, Object>) ce.getArg();
		String tastoSeleziona = (String) args.get("tastoSeleziona");
		tastoSeleziona = (tastoSeleziona == null || tastoSeleziona.isEmpty() ? "N": tastoSeleziona);
		boolean lTastoSeleziona = tastoSeleziona.equals("S");
		if (!lTastoSeleziona) {
			buttonSeleziona.setVisible(false);
		}
		idSelezionato = (Integer) args.get("idselezionato");

		listModelList = new ListModelList();
		lista_Suppinvoicedetail = new SuppinvoicedetailController().getList(null);
		listModelList.addAll(lista_Suppinvoicedetail);
		boxSuppinvoicedetail.setModel(listModelList);
		boxSuppinvoicedetail.setItemRenderer(new SuppinvoicedetailItemRenderer());

		if (idSelezionato!=null && idSelezionato!=0){
		for (int i = 0; i < lista_Suppinvoicedetail.size(); i++) {
			Suppinvoicedetail record = (Suppinvoicedetail)lista_Suppinvoicedetail.get(i);
			if (record.getRecnum()==idSelezionato.intValue()){
				boxSuppinvoicedetail.setSelectedIndex(i);
				break;
			}
		}
	}

}

	public void afterCompose() {

		// wire variables
		ConventionWires.wireVariables(this, this);

		// auto forward
		ConventionWires.addForwards(this, this);

	}

	public void onOK$tbCerca(Event event){
		cerca();
	}

	public void onClick$buttonCerca(Event event) {
		cerca();
	}


	private void cerca(){
		listModelList = new ListModelList();
		lista_Suppinvoicedetail = new SuppinvoicedetailController().getList(tbCerca.getValue());
		listModelList.addAll(lista_Suppinvoicedetail);
		boxSuppinvoicedetail.setModel(listModelList);
		boxSuppinvoicedetail.setItemRenderer(new SuppinvoicedetailItemRenderer());
	}

	public void onDoubleClicked(Event event) {
		if (buttonSeleziona.isVisible()) {
			seleziona();
		} else {
			modifica(false);
		}
	}

	public void onClick$buttonSeleziona(Event event) {

		seleziona();

}

	public void seleziona() {
		if (boxSuppinvoicedetail.getSelectedIndex() >= 0 && buttonSeleziona.isVisible()) {
			Object oSuppinvoicedetail = boxSuppinvoicedetail.getSelectedItem().getAttribute("rigaSuppinvoicedetail");
			String codiceSelezionato = ""+((Suppinvoicedetail) oSuppinvoicedetail).getRecnum();
			Sessions.getCurrent().setAttribute("codiceSuppinvoicedetailSelezionato", codiceSelezionato);
			windowSuppinvoicedetail.detach();
		}
	}

	public void onClick$buttonNuovo(Event event) {
		modifica(true);
	}

	public void onClick$buttonModifica(Event event) {
		modifica(false);
	}

	private void modifica(boolean nuovo){
		try {

			int nIndex = boxSuppinvoicedetail.getSelectedIndex();

			if (!nuovo && nIndex == -1) { // nessuna riga selezionata

				Messagebox.show("Select a row to modify", "Information", Messagebox.OK, Messagebox.INFORMATION);

			} else {
				HashMap map = new HashMap();
				if (nuovo){
					map.put("lNuovo", true);
				}
				map.put("boxSuppinvoicedetail", boxSuppinvoicedetail);
				Window finestraEditSuppinvoicedetail = (Window) Executions.createComponents("/editSuppinvoicedetail.zul", null, map);
				finestraEditSuppinvoicedetail.doModal();
			}

		} catch (SuspendNotAllowedException e) {
			e.printStackTrace();
		}
	}

	public void onClick$buttonCancella(Event event) {
	// TODO Auto-generated method stub

	}

	public class SuppinvoicedetailItemRenderer implements ListitemRenderer {

		public void render(Listitem li, Object data, int index) {
			new Listcell(""+((Suppinvoicedetail) data).getRecnum()).setParent(li);
			new Listcell(""+((Suppinvoicedetail) data).getOrder_()).setParent(li);
			li.setAttribute("rigaSuppinvoicedetail", data);
			ComponentsCtrl.applyForward(li, "onDoubleClick=onDoubleClicked");
		}
	}


// EDIT: NEW/MODIFY
	public void onCreate$windowEditSuppinvoicedetail(Event event) {

		CreateEvent ce = (CreateEvent) ((ForwardEvent) event).getOrigin();
		args = (Map<String, Object>) ce.getArg();

		boxSuppinvoicedetail = (Listbox) args.get("boxSuppinvoicedetail");

		if (args.containsKey("lNuovo")) {
			lNuovo = true;
		} else {
			lNuovo = false;
		}

		if (!lNuovo) {
			SuppinvoicedetailItem = boxSuppinvoicedetail.getSelectedItem().getAttribute("rigaSuppinvoicedetail");
			modSuppinvoicedetail = new SuppinvoicedetailController().refresh((Suppinvoicedetail) SuppinvoicedetailItem);
			SuppinvoicedetailItem = modSuppinvoicedetail;
			windowEditSuppinvoicedetail.setTitle("Edit Suppinvoicedetail");
			//categoryname.setFocus(true);
			//id.setValue(modCategorie.getCategoryid());
			//categoryname.setValue(modCategorie.getCategoryname());
			//description.setValue(modCategorie.getDescription());
			type.setValue(modSuppinvoicedetail.getType());
			note.setValue(modSuppinvoicedetail.getNote());
			suppid.setValue(modSuppinvoicedetail.getSuppid());
			suppvat.setValue(modSuppinvoicedetail.getSuppvat());
			curr.setValue(modSuppinvoicedetail.getCurr());
			unitprice.setValue(modSuppinvoicedetail.getUnitprice());
			stat.setValue(modSuppinvoicedetail.getStat());
			partno.setValue(modSuppinvoicedetail.getPartno());
			productname.setValue(modSuppinvoicedetail.getProductname());
			um.setValue(modSuppinvoicedetail.getUm());
			seq.setValue(modSuppinvoicedetail.getSeq());
			productid.setValue(modSuppinvoicedetail.getProductid());
			quantity.setValue(modSuppinvoicedetail.getQuantity());
			suppvatperc.setValue(modSuppinvoicedetail.getSuppvatperc());
			recnum.setValue(modSuppinvoicedetail.getRecnum());
			custid.setValue(modSuppinvoicedetail.getCustid());
			custname.setValue(modSuppinvoicedetail.getCustname());
			dtup.setValue( (java.util.Date) (modSuppinvoicedetail.getDtUp()));
			exchangerate.setValue(modSuppinvoicedetail.getExchangerate());
			nfysnote.setValue(modSuppinvoicedetail.getNfysNote());
			order.setValue(modSuppinvoicedetail.getOrder());
			paid.setChecked(modSuppinvoicedetail.getPaid());
			paidamount.setValue(modSuppinvoicedetail.getPaidamount());
			suppname.setValue(modSuppinvoicedetail.getSuppname());
			suppnuminvoice.setValue(modSuppinvoicedetail.getSuppnuminvoice());
			tdoc.setValue(modSuppinvoicedetail.getTdoc());
			billitem.setChecked(modSuppinvoicedetail.getBillitem());
			paiddt.setValue( (java.util.Date) (modSuppinvoicedetail.getPaiddt()));
			supptaxed.setValue(modSuppinvoicedetail.getSupptaxed());
			totalamount.setValue(modSuppinvoicedetail.getTotalamount());
			vatarticle.setValue(modSuppinvoicedetail.getVatarticle());
			vatcode.setValue(modSuppinvoicedetail.getVatcode());
		} else {
			windowEditSuppinvoicedetail.setTitle("New Suppinvoicedetail");
			//categoryname.setFocus(true);
		}

	}

	public void onClick$btnAnnulla(Event event){
		windowEditSuppinvoicedetail.detach();
	}

	public void onClick$btnConferma(Event event){
		if (lNuovo){
			modSuppinvoicedetail = new Suppinvoicedetail();
		} else {

		}
		String testoMsg = "";

		modSuppinvoicedetail.setType(type.getValue());
		modSuppinvoicedetail.setNote(note.getValue());
		modSuppinvoicedetail.setSuppid(suppid.getValue());
		modSuppinvoicedetail.setSuppvat(suppvat.getValue());
		modSuppinvoicedetail.setCurr(curr.getValue());
		modSuppinvoicedetail.setUnitprice(unitprice.getValue());
		modSuppinvoicedetail.setStat(stat.getValue());
		modSuppinvoicedetail.setPartno(partno.getValue());
		modSuppinvoicedetail.setProductname(productname.getValue());
		modSuppinvoicedetail.setUm(um.getValue());
		modSuppinvoicedetail.setSeq(seq.getValue());
		modSuppinvoicedetail.setProductid(productid.getValue());
		modSuppinvoicedetail.setQuantity(quantity.getValue());
		modSuppinvoicedetail.setSuppvatperc(suppvatperc.getValue());
		modSuppinvoicedetail.setRecnum(recnum.getValue());
		modSuppinvoicedetail.setCustid(custid.getValue());
		modSuppinvoicedetail.setCustname(custname.getValue());
		modSuppinvoicedetail.setDtUp( (dtup.getValue() != null ? (java.sql.Date) sisi.General.date2Sql(dtup.getValue()): null) );
		modSuppinvoicedetail.setExchangerate(exchangerate.getValue());
		modSuppinvoicedetail.setNfysNote(nfysnote.getValue());
		modSuppinvoicedetail.setOrder(order.getValue());
		modSuppinvoicedetail.setPaid(paid.isChecked());
		modSuppinvoicedetail.setPaidamount(paidamount.getValue());
		modSuppinvoicedetail.setSuppname(suppname.getValue());
		modSuppinvoicedetail.setSuppnuminvoice(suppnuminvoice.getValue());
		modSuppinvoicedetail.setTdoc(tdoc.getValue());
		modSuppinvoicedetail.setBillitem(billitem.isChecked());
		modSuppinvoicedetail.setPaiddt( (paiddt.getValue() != null ? (java.sql.Date) sisi.General.date2Sql(paiddt.getValue()): null) );
		modSuppinvoicedetail.setSupptaxed(supptaxed.getValue());
		modSuppinvoicedetail.setTotalamount(totalamount.getValue());
		modSuppinvoicedetail.setVatarticle(vatarticle.getValue());
		modSuppinvoicedetail.setVatcode(vatcode.getValue());

		if (lNuovo){
			new SuppinvoicedetailController().add(modSuppinvoicedetail);
			testoMsg = "New Suppinvoicedetail: "+ General.trimNull( modSuppinvoicedetail.getOrder_() );
		} else {
			new SuppinvoicedetailController().update(modSuppinvoicedetail);
			testoMsg = "Updating Suppinvoicedetail: "+ General.trimNull( modSuppinvoicedetail.getOrder_() );
		}

		General.showNotify( testoMsg, windowEditSuppinvoicedetail);

		windowEditSuppinvoicedetail.detach();

		// aggiorna listbox

		if (lNuovo){
			ListModelList lml = (ListModelList) boxSuppinvoicedetail.getListModel();
			lml.add(modSuppinvoicedetail);
			boxSuppinvoicedetail.setSelectedIndex(lml.indexOf(modSuppinvoicedetail));
		} else {
			ListModelList lml = (ListModelList) boxSuppinvoicedetail.getListModel();
			Object oSuppinvoicedetailItem = boxSuppinvoicedetail.getSelectedItem().getAttribute("rigaSuppinvoicedetail");
			if (lml.indexOf(oSuppinvoicedetailItem) == -1) {
				// non trovato... quindi non lo aggiorno
			} else {
				lml.set(lml.indexOf(oSuppinvoicedetailItem), modSuppinvoicedetail);
			}
		}
	}

}
//
// fine
